package com.myproject.music.controller;


import com.myproject.music.BizException;
import com.myproject.music.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Api(tags = "文件操作(0515郭彦良)")
@RestController
public class FileController {
    @Value("E:/学习专用/web架构/ruoyi/uploadPath")
    private String UPLOAD_FOLLDER;
    @ApiOperation(value="歌手图片上传",notes = "将歌手图片上传到服务器")
    @PostMapping(value = "/upload")
    public AjaxResult upload(@RequestPart("file")MultipartFile file){
        if(file.isEmpty()){
            return AjaxResult.error("上传文件不能为空");

        }
        String fileName=file.getOriginalFilename();
        File path=new File(UPLOAD_FOLLDER);
        if(!path.exists()){
            path.mkdir();
        }
        File dest=new File(UPLOAD_FOLLDER+"/"+fileName);
        try {
            file.transferTo(dest);
            String url = "/private" + fileName;
            return AjaxResult.success("文件上传成功").put("url", url);
        }
        catch(IOException e){
            throw new BizException(500,"文件上传失败");
        }
    }
}
